/**
 * @param {String} url 请求地址
 * @param {Object} data 请求数据
 * @param {String} method 请求方法
 * @param {Number} contentType 请求内容类型 1=json  2=form
 * @param {Number} channel 请求平台 1云平台 2供应链
 */

import { useLoginStore, useUserStore,useSocketStore,useStartTodoPolling } from "@/pinia-store/user.js";

function service(options = {}, loginStatus, ) {
	let baseUrl = "";
	if(!options.channel) options.channel = 1;

	switch(options.channel) {
		case 1: baseUrl = "https://cloud.mfarmers.com/dist/interface/public/index.php";// 云平台
		// case 1: baseUrl = "http://121.229.203.227:85/dist/interface/public/index.php";// 云平台
		// case 1: baseUrl = "http://host.dev.ypt.com/index.php";// 云平台
		break;
		case 2: baseUrl = "https://cloud.mfarmers.com/"; // 供应链
		break;
		case 3: baseUrl = "https://cloud.mfarmers.com/"; // 园区管理软件
		break;
		case 4: baseUrl = "https://cloud.mfarmers.com"; // 人脸识别
		break;
		case 5: baseUrl = "https://chat.mfarmers.com:88/faceIm.php"; // 聊天
		break;
		case 6: baseUrl = "http://121.229.203.227:83"; // 工程管理软件
		break;
		case 7: baseUrl = "https://net.mfarmers.com:89"; // nextcloud
		break;
	}

	options.url = `${baseUrl}${options.url}`;
	
	// token
	const loginStore = useLoginStore();
	const socketStore = useSocketStore()
	const startTodoPolling = useStartTodoPolling()
	let token = loginStore.token;

	// 不需要登录的接口
	if (loginStatus) {
		options.header = {
			'content-type': options.contentType === 1 ? 'application/x-www-form-urlencoded' : 'application/json',
		};
	} else {
		if (token) {
			options.header = {
				'content-type': options.contentType === 1 ? 'application/x-www-form-urlencoded; charset=UTF-8' : 'application/json',
			};
			if(options.method == 'GET' || options.method == 'get') {
				
				if(options.channel == 7) {
					options.header = {
						"accept": "application/json, text/plain, */*"
					};
				}
				// options.url = options.url
			}else {
				if(options.channel !== 5) {
					options.data.token = token;
				}
			}
		} else {
			// 缓存被系统清空
			console.log('缓存丢失');
		}
	}

	return new Promise((resolved, rejected) => {
		options.success = (res) => {
			// 如果请求回来的状态码不是200则执行以下操作
			if (res?.data?.status == 0 && (res?.data?.msg == "登录失效" || res?.data?.msg == "缺少TOKEN参数")) {
				const userStore = useUserStore();
				let worker_id = userStore?.user?.worker_id;

				// 非成功状态码弹窗
				uni.showToast({
					icon: 'none',
					title: `登录状态已失效`
				});

				uni.reLaunch({
					url: "/pages/user/login"
				});
				

        try {
          uni.clearStorageSync();
        } catch (error) {
          uni.removeStorageSync('token');
          uni.removeStorageSync('user');
          uni.removeStorageSync('OAcurrentTab');
          uni.removeStorageSync('audioUnlocked');
          uni.removeStorageSync('_ws');
          uni.removeStorageSync('friend');
          uni.removeStorageSync('groupMsg');
          uni.removeStorageSync('nowDialogId');
        }
				startTodoPolling.restart()
				socketStore.handleClose()
				// #ifdef APP-PLUS
				SysUser.deleteById(worker_id);
				// #endif
				// 返回错误信息
				rejected(res)
			} else {
				// 请求回来的状态码为200则返回内容
				resolved(res.data)
			}
		};
		options.fail = (err) => {
			console.log('错误信息', err)
			// 请求失败弹窗
			// uni.showToast({
			// 	icon: 'none',
			// 	duration: 3000,
			// 	title: '服务器错误,请稍后再试'
			// });
			rejected(err);
		};
		uni.request(options);
	});
}

export default service;

export  const COMMON_API_BASE_URL = 'https://cloud.mfarmers.com';
// export  const COMMON_API_BASE_URL = 'http://121.229.203.227:85';
